package servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.yunhe.dao.BoardDao;
import cn.yunhe.dao.TopicDao;
import cn.yunhe.dao.impl.BoardDaoImpl;
import cn.yunhe.dao.impl.TopicDaoImpl;
import cn.yunhe.entity.Board;
import cn.yunhe.entity.Topic;
import cn.yunhe.entity.User;
import cn.yunhe.util.DateUtil;

/**
 * Servlet implementation class addBoard
 */
@WebServlet("/addBoard")
public class addTopic extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addTopic() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			TopicDao topicDao=new TopicDaoImpl();
			String  title= request.getParameter("title");//获取发帖标题	
			System.out.println(title+"-------------------------");
			String  TitleContent= request.getParameter("TitleContent");//获取发帖内容
			System.out.println(TitleContent+"--------------------");
			String  bidStr= request.getParameter("bid");//获取所属板块		
			System.out.println(bidStr+"############################");
			int bid=-1;
			if(bidStr!=null && !bidStr.equals("")){
				 bid=Integer.parseInt(bidStr);
			}
			/*if(title==null){//接收第一个空值;
				System.out.println("--");
			}*/
			User user=(User)request.getSession().getAttribute("CURR_USER");
			
			BoardDao boardDao = new BoardDaoImpl();
		   // Board board = boardDao.getBoardById(bid);
			Board board=new Board();
			board.setId(bid);
			
			//Topic topic=new Topic(title,TitleContent,DateUtil.dateTimeToString(new Date()),user,board);
		    Topic topic=new Topic();
		    topic.setTitle(title);
		    topic.setContent(TitleContent);
		    topic.setBoard(board);
		    topic.setCreateTime(DateUtil.dateTimeToString(new Date()));
		    topic.setUser(user);
			
			boolean result=topicDao.addTopic(topic);
		    String str=null;
		    if(result){
		    	str="发帖成功";
		    }else{
		    	str="发帖失败";
		    }
			//request.setAttribute("board",board);
			request.getRequestDispatcher("getTopices?bid="+bid).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
